﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace MylambdaLinqDemo.Linq
{
    /// <summary>
    /// Linq
    /// </summary>
    public class LinqShow
    {
        #region Data Init
        private static List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="赵亮",
                    ClassId=2,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="再努力一点",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="王炸",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="疯子科学家",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="灭",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="黑骑士",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="故乡的风",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="晴天",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="旭光",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="oldkwok",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="乐儿",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="暴风轻语",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一个人的孤单",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="小张",
                    ClassId=2,
                    Age=30
                },
                 new Student()
                {
                    Id=3,
                    Name="阿亮",
                    ClassId=3,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="关耳",
                    ClassId=4,
                    Age=30
                }
                  ,
                  new Student()
                {
                    Id=4,
                    Name="耳机侠",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Wheat",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Heaven",
                    ClassId=4,
                    Age=22
                },
                  new Student()
                {
                    Id=4,
                    Name="等待你的微笑",
                    ClassId=4,
                    Age=23
                },
                  new Student()
                {
                    Id=4,
                    Name="畅",
                    ClassId=4,
                    Age=25
                },
                  new Student()
                {
                    Id=4,
                    Name="混无痕",
                    ClassId=4,
                    Age=26
                },
                  new Student()
                {
                    Id=4,
                    Name="37度",
                    ClassId=4,
                    Age=28
                },
                  new Student()
                {
                    Id=4,
                    Name="新的世界",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="Rui",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="帆",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="肩膀",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="孤独的根号三",
                    ClassId=4,
                    Age=30
                },
                  new Student()
                {
                    Id=4,
                    Name="振轩儿",
                    ClassId=4,
                    Age=30
                }
            };
            #endregion
            return studentList;
        }
        #endregion

        /// <summary>
        /// 以下全部都是有Where后，简单操作；
        /// 假设：没有这个Where方法呢？
        /// 
        /// </summary>

        public static void Show()
        {
            List<Student> studentList = GetStudentList();
            //1.名称包含赵亮
            {
                //这句话的执行，得到的记过，是筛选出赵亮这个人  Where的使用，就是Linq的一种。
                 var sList = studentList.Where(s => s.Name.Contains("赵亮")).ToList();
                 
                //如果没有Linq  希望筛选出赵亮这个人---只能循环--但是循环---出现了大量的重复代码；
                //能不能封装下？ 当然可以的；
                // 
                #region MyRegion 
                List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的； 
                foreach (var student in studentList)
                {
                    //if (student.Name.Contains("赵亮"))
                    if (IsOk1(student))
                    {
                        students1.Add(student);
                    }
                }
                //三个循环，就方法不一样，如果能够把方法当做参数来传递---刚好解决这个问题；
                // 就是委托来传递

                #endregion

                //Func<Student, bool> func = new Func<Student, bool>(IsOk2);

                //Func<Student, bool> func = new Func<Student, bool>(student =>
                //{
                //    return student.Name.Contains("赵亮");
                //}); 
                //Func<Student, bool> func = new Func<Student, bool>(student => student.Name.Contains("赵亮"));


                //Func<Student, bool> func = student => student.Name.Contains("赵亮"); 
                //List<Student> students2 = MethodExtension.RichardWhere(studentList, func);

                //Func<Student, bool> func = student => student.Name.Contains("赵亮");

                List<Student> students2 = MethodExtension.RichardWhere(studentList, student => student.Name.Contains("赵亮"));

                List<Student> students3 = studentList.RichardWhere(student => student.Name.Contains("赵亮"));


                var sList1 = studentList.Where(s => s.Name.Contains("赵亮")).ToList();

                List<Teacher> teachers = null; 
                var sList2 = studentList.Where(s => s.Name.Contains("赵亮")).ToList();
                teachers.Where(t => t.Id == 1); 
                //teachers.RichardWhere(t=>t.Id==1);

                //全模拟了一个Where方法出来？

            }
            //2.筛选中集合中名称的长度小于4的----Where
            {

                //var sList = studentList.Where(s => s.Name.Length < 4).ToList();

                #region MyRegion 

                List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的；
                foreach (var student in studentList)
                {
                    //if (student.Name.Length < 4)
                    if (IsOk2(student))
                    {
                        students1.Add(student);
                    }
                }
                #endregion

                List<Student> students2 = MethodExtension.RichardWhere(studentList, student => student.Name.Length < 4);
                 
                List<Student> students3 = studentList.RichardWhere(student => student.Name.Length < 4);


            }
            //3. N个条件叠加，随着需求的不同，我根本不知道究竟需要多个个条件；  
            {
                //var sList = studentList.Where(item => item.Id > 1
                //            && item.Name != null
                //            && item.ClassId > 2
                //            && item.Age > 20).ToList();


                #region MyRegion

                List<Student> students1 = new List<Student>(); //经过计算，这个集合和sList结果一致的；
                foreach (var item in studentList)
                {
                    //if (item.Id > 1 && item.Name != null && item.ClassId > 2 && item.Age > 20)
                    if (IsOk3(item))
                    {
                        students1.Add(item);
                    }
                }
                #endregion

                //Func<Student, bool> func = new Func<Student, bool>(IsOk3);
                //List<Student> students2 = MethodExtension.RichardWhere(studentList, func);


                List<Student> students2 = MethodExtension.RichardWhere(studentList, item => item.Id > 1 && item.Name != null && item.ClassId > 2 && item.Age > 20);


                List<Student> students3 = studentList.RichardWhere(item => item.Id > 1 && item.Name != null && item.ClassId > 2 && item.Age > 20);
            }


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public static bool IsOk1(Student student)
        {
            return student.Name.Contains("赵亮");
        }


        public static bool IsOk2(Student student)
        {
            return student.Name.Length < 4;
        }

        public static bool IsOk3(Student item)
        {
            return item.Id > 1 && item.Name != null && item.ClassId > 2 && item.Age > 20;
        }
    }
}